/**
 * indexes.js
 * Get the filelist with Apache "mod_autoindex" Module and jQuery(or Prototype or No libs).
 * 
 * Version 0.1
 * 
 * Copyright (c) 2009 tnantoka(http://blog.bornneet.com/)
 * The MIT License(http://indexes.bornneet.com/license.txt)
 */

var Indexes = function() {
	this.init.apply(this, arguments);
};

Indexes.prototype = {

	lib: null,
	xmlHttp: null,

    init: function() {
		if(window.jQuery) {
			this.lib = this.j;
		} else if(window.Prototype) {
    		this.lib = this.p;
    	} else {
			try {
				this.xmlHttp = new XMLHttpRequest();
			} catch(e) {
				try {
					this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
				} catch(e) {
					try {
						this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					} catch(e) {
					}
				}
			}
        	this.lib = this.t;
        }
    },
    
    checkPath: function(path) {
    	return /\/$/.test(path) ? path : path + '/';
    },
    
    getIndex: function(path, parent) {
    	if(typeof path == 'object') {
    		parent = path.parent;
    		path = path.path;
    	}
    	path = this.checkPath(path);
    	return this.lib.index.apply(this, [path, false, false, parent]);
    },
    
    index: function(path, func, async, parent) {
    	if(typeof path == 'object') {
    		func = path.func;
    		async = path.async;
    		parent = path.parent;
    		path = path.path;
    	}
    	if(typeof func == 'undefined') {
    		return this.getIndex(path, parent);
    	}
    	if(typeof async == 'undefined') {
    		async = true;
    	}
    	path = this.checkPath(path);
    	this.lib.index.apply(this, [path, func, async, parent]);
    },
    
    makeIndex: function(data, parent) {

		var a = [];
		var files = data.match(/<pre[\s\S]*\/pre>/i, '')[0].match(/<a[\s\S]+?\/a>/ig);
		for(var i = parent ? 4 : 5; i < files.length; i++) {
			files[i].match(/href[\s\S]*=[\s\S]*"([\s\S]+)"/i);
			var file = RegExp.$1;
			a.push(file);
		}

		return a;
    },
    
    j: { 
    
    	index: function(path, func, async, parent) {
			var a = [];
			var self = this;
			jQuery.ajax({
				url: path,
				async: async,
				success: function(data) {
					a = self.makeIndex(data, parent);
	    			if(func) func(a);
    			}
    		});
    		
    		if(!func) return a;
    	}
    },

    p: {
    
    	index: function(path, func, async, parent) {
    	
			var a = [];
			var self = this;
			new Ajax.Request(path, {
				method: 'GET',
				asynchronous: async,
				onSuccess: function(data, parent) {
					a = self.makeIndex(data.responseText);
	    			if(func) func(a);
    			}
    		});

    		if(!func) return a;
    	}
    },

    t: {    

    	index: function(path, func, async, parent) {
    	
			var a = [];
			var self = this;
			this.xmlHttp.open('GET', path, async);
			
	    	if(async) {
				this.xmlHttp.onreadystatechange = function() {
					if(self.xmlHttp.readyState == 4 && self.xmlHttp.status == 200) {
						a = self.makeIndex(self.xmlHttp.responseText, parent);
	    				if(func) func(a);
	 				}
				};
			}
			
			this.xmlHttp.send(null);

			if(!async) {
				a = this.makeIndex(this.xmlHttp.responseText);
	    		if(func) func(a);
	   		}
	   		
	   		if(!func) return a;
    	}
    },

	isDir: function(path) {
		return /\/$/.test(path);
	}, 

	getFileName: function(path) {
		return path.match(/^(\.?[^\.\/]+)/) ? RegExp.$1 || RegExp.$2 : '';
	}, 	

	getExt: function(path) {
		return path.match(/\.(.+)$|(\/)$/) ? RegExp.$1 || RegExp.$2 : '';
	}
};

